package com.ztz.mynewdemo.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.ztz.mynewdemo.MessageEvent;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.bean.MyDatabaseHelper;

import org.greenrobot.eventbus.EventBus;

public class TopActivity extends BaseActivity {
    public static final String TOP_ID="id";
    public static final String TITLE="title";
    public static final String DATE="date";
    public static final String CATEGORY="category";
    public static final String AUTHOR="author";
    public static final String IMAGE="image";
    WebView webView;
    Button btn;
    MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_top);
        webView=(WebView)findViewById(R.id.top_webView);
        btn=(Button)findViewById(R.id.btn_collect);
        setToolbar("头条");
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.setWebViewClient(new WebViewClient());
        final String id=getIntent().getStringExtra(TOP_ID);
        final String title=getIntent().getStringExtra(TITLE);
        final String date=getIntent().getStringExtra(DATE);
        final String category=getIntent().getStringExtra(CATEGORY);
        final String author=getIntent().getStringExtra(AUTHOR);
        final String image=getIntent().getStringExtra(IMAGE);
        webView.loadUrl(id);
        dbHelper=new MyDatabaseHelper(this,"CollectionStore.db",null,4);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MessageEvent("你好"));
//                SharedPreferences.Editor editor= PreferenceManager.getDefaultSharedPreferences(TopActivity.this).edit();
//                editor.putString("url",id);
//                editor.apply();
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("type",1);
                values.put("title",title);
                values.put("date",date);
                values.put("category",category);
                values.put("author_name",author);
                values.put("image",image);
                values.put("url",id);
                db.insert("Collection",null,values);
            }
        });
    }





    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
